import type { ConfigEnv, UserConfig } from 'vite'
import { nodeResolve } from '@rollup/plugin-node-resolve'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import UnoCSS from 'unocss/vite'
import AutoImportRollup from 'unplugin-auto-import/rollup'
import { defineConfig } from 'vite'
import dts from 'vite-plugin-dts'

export default defineConfig((env: ConfigEnv) => {
  return {
    plugins: [
      vue(),
      vueJsx(),
      UnoCSS('../../uno.config.ts'),
      dts({
        // 自动复制d.ts
        copyDtsFiles: true,
        insertTypesEntry: true,
        outDir: 'dist/dts',
        include: ['./src', './auto-imports.d.ts'],
      }),
    ],
    build: {
      outDir: 'dist',
      minify: 'terser',
      assetsInlineLimit: 1024 * 100, // 小于100kb的静态资源打包时将内联为base64
      lib: {
        entry: './src/index.ts',
        name: 'fetm-components',
        formats: ['es'],
      },
      rollupOptions: {
        // 第三方库/样式取消打包, 在peerDependencies中加入了该依赖, 会自动引入
        external: [
          '@codemirror/theme-one-dark',
          '@codemirror/state',
          '@codemirror/view',
          '@codemirror/lang-html',
          '@codemirror/lang-java',
          '@codemirror/lang-javascript',
          '@codemirror/lang-json',
          '@codemirror/lang-markdown',
          '@codemirror/lang-sql',
          '@codemirror/lint',
          '@element-plus/icons-vue',
          '@iconify/iconify',
          '@vueuse/core',
          '@wangeditor/editor',
          '@wangeditor/editor-for-vue',
          '@zxcvbn-ts/core',
          'codemirror',
          'diff',
          'diff2html',
          'diff2html/lib/ui/js/diff2html-ui',
          'echarts',
          'echarts-wordcloud',
          'element-plus',
          'fetm-hooks',
          'fetm-utils',
          'lodash-es',
          'markdown-it-link-attributes',
          'md-editor-v3',
          'vue',
          'vue-codemirror',
          'vue-json-pretty',
          'vue-router',
          'vue-types',
          'xgplayer',
          'xgplayer-dash',
          'xgplayer-flv',
          'xgplayer-hls',
          'font-awesome/css/font-awesome.min.css',
        ],
        output: {
          exports: 'named',
        },
        plugins: [
          nodeResolve(),
          // 自动导入vue等内部的相关函数
          AutoImportRollup({
            imports: ['vue', '@vueuse/core'],
            dts: './auto-imports.d.ts',
          }),
        ],
      },
    },
  } as UserConfig
})
